
from xml.sax.handler import ContentHandler
from our_string import our_string
import database
import element

class storageHandler(ContentHandler):

    def __init__(self,file_name,base):
        
        self.document = base.insert_doc(file_name)
        self.current_text = our_string("")
        self.first = 1
        self.base = base

    
    def get_doc_id(self):
        
        return self.document.get_document_id()


    def startElement(self,name,attrs):
        
        if self.first == 1:
            self.current = element.element(name)
            self.first = 0
        else:
            new = element.element(name)
            new.set_parent(self.current)
            self.current = new

        self.current_text = our_string("")

    def endElement(self,name):

        if self.current.is_leaf_node() != 0:
            self.current.end_leaf(self.document)
            self.base.insert_leaf(self.document,self.current,self.current_text.del_spaces())
            
        else:
            self.current.end_ancestor()
            self.base.insert_ancestor(self.document,self.current)

        self.current = self.current.get_parent()

    def characters(self,content):

        self.current_text = our_string(self.current_text + content)

import xml.sax
data_base = database.database()
teste = storageHandler("../a.xml",data_base)
parser = xml.sax.make_parser()
parser.setContentHandler(teste)
parser.parse("../a.xml")

teste2 = storageHandler("../b.xml",data_base)
parser = xml.sax.make_parser()
parser.setContentHandler(teste2)
parser.parse("../b.xml")

data_base.find_best_matching_subtrees(teste.get_doc_id(),teste2.get_doc_id(),0.5)
